<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Member extends Model
{
    protected $table = 'v_members';

    //不可批量赋值的属性
    protected $guarded = ['id'];

    //状态
    const STATUS_Y = 1;
    const STATUS_N = 2;

    //角色
    const ROLE_DRIVER = 10; // 司机
    const ROLE_OWNER = 20; // 货主
    const ROLE_SHIPPER = 21; // 发货人
    const ROLE_RECEIVER = 22; // 收货人
    const ROLE_FINANCE = 23; // 财务
    const ROLE_ORDER = 24; // 发单

    public static $roleDsc = [
        self::ROLE_DRIVER => '司机',
        self::ROLE_OWNER => '货主',
        self::ROLE_SHIPPER => '发货人',
        self::ROLE_RECEIVER => '收货人',
        self::ROLE_FINANCE => '财务',
        self::ROLE_ORDER => '发单',
    ];

    // 可用的角色
    const USABLE_ROLE = [
        self::ROLE_DRIVER,
        self::ROLE_OWNER,
        self::ROLE_FINANCE,
        self::ROLE_ORDER
    ];

    //绑定关系类型
    const RELATION_TYPE_DAY = 10; //临时的
    const RELATION_TYPE_FORMAL = 20; // 正式的

    // 用户终端
    const USER_PLATFORM_ANDROID = 1; // 安卓
    const USER_PLATFORM_IOS = 2; // IOS
    const USER_PLATFORM_WXXCX = 3; // 微信小程序

    const USER_PLATFORMS = [
        self::USER_PLATFORM_ANDROID => '安卓',
        self::USER_PLATFORM_IOS => 'IOS',
        self::USER_PLATFORM_WXXCX => '微信小程序',
    ];


    //司机详细信息
    public function driver()
    {
        return $this->hasOne('App\Models\Driver', 'member_id', 'id');
    }

    //货主详细信息
    public function owner()
    {
        return $this->hasOne('App\Models\Owner', 'member_id', 'id');
    }

    //审核信息
    public function audit()
    {
        return $this->hasOne('App\Models\MemberAudit', 'member_id', 'id');
    }

    //员工信息
    public function childs()
    {
        return $this->belongsToMany('App\Models\Member', 'v_member_relation', 'parent_id', 'member_id');
    }

    //普通员工角色信息
    public function relation()
    {
        return $this->hasMany('App\Models\MemberRelation', 'member_id', 'id');
    }

    public function changeRoleText($role_id)
    {
        $role_text = '';
        switch ($role_id) {
            case 10:
                $role_text = '司机';
                break;
            case  20:
                $role_text = '货主';
                break;
            default  :
                $role_text = '员工';
        }
        return $role_text;
    }

    /**
     * 获取某个货主下的所有司机
     */
    public static function getListByOwnerId($owner_id)
    {
        return self::where('owner_id', $owner_id)->where('status', 1)->get()->toArray();

    }
}
